package com.xblibo.xruth.common;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.mlw.vlh.ValueList;
import net.mlw.vlh.ValueListHandler;
import net.mlw.vlh.ValueListInfo;
import net.mlw.vlh.web.ValueListRequestUtil;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.xblibo.xruth.entity.User;
import com.xblibo.xruth.security.SecurityUserHolder;
@SuppressWarnings("unchecked")
public class BaseController{
	@Resource 
	protected ValueListHandler valueListHandler;
	
	protected ValueListInfo valueListInfo;
	
	protected Map<String,Object> params = new HashMap<String,Object>();
	
	protected User getCurrentUser(){
		return SecurityUserHolder.getCurrentUser();
	}
	
	protected ValueList getValueList(String queryId,Map params){
		valueListInfo = new ValueListInfo(params);
		return valueListHandler.getValueList(queryId, valueListInfo);
	}
	
	protected ValueList getValueList(String queryId,Map params,HttpServletRequest request){
		valueListInfo = ValueListRequestUtil.buildValueListInfo(request);
		valueListInfo.getFilters().putAll(params);
		return valueListHandler.getValueList(queryId, valueListInfo);
	}
	
	
	@InitBinder
	public void InitBinder(WebDataBinder dataBinder)
	{
		dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
	}
	
	protected String getToday(){
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return simpleDateFormat.format(new Date());
	}
	
}
